package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;

import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.platform.service.IBusTeamService;
import com.yuyou.fn.tradingservice.service.IServiceCategoryService;
import com.yuyou.fn.tradingservice.service.IServiceImageService;
import com.yuyou.fn.tradingservice.vo.ServiceProductVo;
import org.springframework.stereotype.Service;

import com.yuyou.fn.tradingservice.mapper.ServiceProductMapper;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.service.IServiceProductService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * ServiceProduct 表数据服务层接口实现类
 *
 */
@Service
public class ServiceProductServiceImpl extends SuperServiceImpl<ServiceProductMapper, ServiceProduct,Long> implements IServiceProductService {

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private IServiceCategoryService categoryService;

    @Resource
    private IServiceImageService serviceImageService;

    @Resource
    private ICampusService campusService;

    @Override
    public void saveServiceProduct(ServiceProductVo productVo, String productImages) {
        ServiceProduct serviceProduct = new ServiceProduct();
        BeanUtils.copyProperties(serviceProduct, productVo);
        fillExtraData(serviceProduct);
        if(serviceProduct.getProductId() == null) {
            if (productVo.getCategoryId()!=null) {
                serviceProduct.setType(categoryService.findById(productVo.getCategoryId()).getType());
            }
            serviceProduct.setCreateTime(TimeUtils.now());
            serviceProduct.setUpdateTime(TimeUtils.now());
            this.insertSelective(serviceProduct);
        } else {
            serviceProduct.setUpdateTime(TimeUtils.now());
            this.updateSelectiveById(serviceProduct);
        }
        //生成图片
        serviceImageService.generateProductImages(serviceProduct,productImages);
    }

    @Override
    public Page<ServiceProduct> findProductForPage(int pageNo, int pageSize, Long categoryId, Long busTeamId, String productName) {
        Example<ServiceProduct> example = ServiceProduct.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        if(categoryId != null) {
            criteria.andEqualTo("categoryId", categoryId);
        }
        if(busTeamId != null) {
            criteria.andEqualTo("busTeamId", busTeamId);
        }
        if(!"".equals(productName)|| productName!=null) {
            criteria.andLike("productName", "%"+productName+"%");
        }
        return findPageByExample(example, pageNo, pageSize);
    }


    @Override
    public Page<ServiceProduct> findProductForPage(ServiceProductVo productVo, int pageNo, int pageSize) {
        Example<ServiceProduct> example = ServiceProduct.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();

        CriteriaUtils.fillCondition(criteria, productVo);

        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public void deleteProduct(Long categoryId) {
        Example<ServiceProduct> example = ServiceProduct.createExample();
        example.createCriteria().andEqualTo("categoryId", categoryId);
        this.deleteByExample(example);
    }

    @Override
    public List<ServiceProduct> findProductsByCategoryId(Long categoryId) {
        Example<ServiceProduct> example = ServiceProduct.createExample();
        example.createCriteria().andEqualTo("categoryId", categoryId);
        return this.findListByExample(example);
    }

    @Override
    public List<ServiceProduct> productDetail(Long categoryId) {
        List<Long> categoryIds = categoryService.getAllChildCategory(categoryId);
        categoryIds.add(categoryId);
        List<ServiceProduct> productList = new ArrayList<>();
        for (Long id : categoryIds) {
            Example<ServiceProduct> example = ServiceProduct.createExample();
            example.createCriteria().andEqualTo("categoryId", id);
            productList.addAll(this.findListByExample(example));
        }
        return productList;
    }

    public void fillExtraData(ServiceProduct product) {
        if (product.getBusTeamId() != null) {
            product.setBusTeamName(busTeamService.findTeamNameById(product.getBusTeamId()));
        } else {
            product.setBusTeamId(0L);
            product.setBusTeamName("");
        }
        if (product.getCategoryId() != null) {
            product.setCategoryName(categoryService.findCategoryNameById(product.getCategoryId()));
        } else {
            product.setCategoryId(0L);
            product.setCategoryName("");
        }
        if (product.getCampusId()!=null){
            product.setCampusName(campusService.findCampusNameByCampusId(product.getCampusId()));
        }else {
            product.setCampusName("");
            product.setCampusId(0L);
        }
    }

}